package cn.huixh.blindbox.common;

import cn.huixh.blindbox.exception.BaseErrorInfoInterface;
import com.alibaba.fastjson.JSON;
import lombok.Data;

/**
 * @author huixh
 * @version 1.0
 * @date 2022/5/6 18:24
 */
@Data
public class ResultData< T > {
    /**
     * 操作状态码
     */
    private int status;
    /**
     * 操作状态信息
     */
    private String message;
    /**
     * 操作结果数据
     */
    private T data;
    /**
     * 操作时间戳
     */
    private long timestamp;

    public ResultData() {
        this.timestamp = System.currentTimeMillis();
    }

    public ResultData(BaseErrorInfoInterface errorInfo) {
        this();
        this.status = errorInfo.getResultCode();
        this.message = errorInfo.getResultMsg();
    }


    public static < T > ResultData< T > success(T data) {
        ResultData< T > resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC100.getResultCode());
        resultData.setMessage(ReturnCode.RC100.getResultMsg());
        resultData.setData(data);
        return resultData;
    }

    public static < T > ResultData< T > error(int code, String message) {
        ResultData< T > resultData = new ResultData<>();
        resultData.setStatus(code);
        resultData.setMessage(message);
        return resultData;
    }

    public static < T > ResultData< T > error(BaseErrorInfoInterface errorInfo) {
        ResultData< T > resultData = new ResultData<>();
        resultData.setStatus(errorInfo.getResultCode());
        resultData.setMessage(errorInfo.getResultMsg());
        resultData.setData(null);
        return resultData;
    }

    public static < T > ResultData< T > error(String message) {
        ResultData< T > resultData = new ResultData<>();
        resultData.setStatus(ReturnCode.RC999.getResultCode());
        resultData.setMessage(message);
        resultData.setData(null);
        return resultData;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}
